package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.api.exceptions.BaseExceptions;
import com.qf.lanzhou.shop.api.service.UserService;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Nerly
 * @Date 2021/1/13 10:08
 * @JDK 1.8
 * @Version 1.0
 */
@RestController
@Api(tags = "用户接口")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @GetMapping("/getUser")
    @ApiOperation(value = "根据sessionId获取用户")
    public BaseRespVO selectById(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (null == user) {
            throw new BaseExceptions(301, "用户未登录");
        }
        User result = userService.selectById(user);
        BaseRespVO baseRespVO = convertSuccessResult(result);
        return baseRespVO;
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "根据用户id修改用户")
    public BaseRespVO updateUser(User user) {
        int result = userService.updateUser(user);
        BaseRespVO baseRespVO = convertSuccessResult(result);
        return baseRespVO;
    }
}
